/*! provide class of separate thread open/close , read/write comm port data with strings
*/
#ifndef CONNECT_THREAD_H
#define CONNECT_THREAD_H

#include"serial_api.h"
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include <QtCore/QString>
#include "xbeestruct.h"
typedef struct  vector_of_uchars
{
	std::vector<unsigned char> data;
}vector_of_uchars;
class commp_thread: public QObject//QThread
{
	Q_OBJECT

	

	//UNUSED bool is_connection_opened;
	bool is_reading;
	bool is_writing;
	QString stored_data_waiting_endl;//for string reading mode
public:
	QString comm_name;// name of connection file
	commp_thread(QString comm_name,TTYSettings inp_settings,QObject * parent=0);
	commp_thread(QString comm_name,TTYSettings inp_settings,QString endlsymbol,QObject * parent=0);
	~commp_thread();
	QString endlsymbol;
	
	bool simple_or_xbee; //defines if we send message with xbee module or simple line
	//on xbee module we send on selected adresses, //TODO and if no adress presented, broadcast
	void ReadQStr(QString & str_in);
	
	//void run();
	
	private:
		bool ConnectToSerial();//using commname conect to serial port
		int read_interval;//ms
		QTimer * CommTimer;
		TTY winSerialConnector;
		TTYSettings this_thread_settings;
		bool is_timer_inited;
	private slots:
		//periodic function inside separate thread
		void OnTimerHit();
	public slots:
		void SendQStr(QString str_in);
		void SendQStr1(vector_of_uchars str_in);
			//init function, called with creation of separate thread inside it
		void ForceTimer(); // connect to comm port, start read/write timer
		void Selfdestruct();// finish work and clear object
		void SetConnectionMode(int new_mode);// 0 - string mode //parse strings with endl; 2 -  Xbee API mode //parse xpee structure here on read/write
		signals:
			//void IsConnected();
			void StringCatched(QString recieved_string);
			void PacketCatched(XBeePacket recieved_packet);
			void ConnectionModeIsSet(int new_mode);
			void ConnectionLost();
			void FailToConnect();

};
#endif
